package com.ruowei.modules.sys.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.IOException;
import java.util.Collections;

public class PdfUtils {

    /**
     * @param i
     * @apiNote 加空格
     * @author 董兴
     * @date 2020-03-19
     */
    public static String addSpace(int i) {
        return String.join("", Collections.nCopies(i, "\u00a0"));
    }

    public static PdfPCell createCell(String text, Font font, float height, int horizontal, int vertical) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(height);
        cell.setHorizontalAlignment(horizontal);
        cell.setVerticalAlignment(vertical);
        return cell;
    }

    public static PdfPCell createCell(String text, Font font, float height) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(height);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }

    public static PdfPCell createCell(String text) {
        PdfPCell cell = new PdfPCell(new Paragraph(text));
        cell.setMinimumHeight(40);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }

    public static PdfPCell createCell(String text, float height) {
        PdfPCell cell = new PdfPCell(new Paragraph(text));
        cell.setMinimumHeight(height);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }

    public static PdfPCell createCell(String text, Font font) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(40);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }

    public static PdfPCell createCell(String text, Font font, Boolean isHorizontal, Boolean isVertical) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(40);
        if (isHorizontal) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
        if (isVertical) {
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        }
        return cell;
    }

    public static PdfPCell mergeColAndRow(float height, int col, int row) {
        PdfPCell cell = new PdfPCell();
        cell.setMinimumHeight(height);
        cell.setColspan(col);
        cell.setRowspan(row);
        return cell;
    }

    public static PdfPCell mergeColAndRow(int horizontalAlignment, int verticalAlignment, float height, int col, int row) {
        PdfPCell cell = new PdfPCell();
        cell.setMinimumHeight(height);
        cell.setHorizontalAlignment(horizontalAlignment);
        cell.setVerticalAlignment(verticalAlignment);
        cell.setColspan(col);
        cell.setRowspan(row);
        return cell;
    }

    public static PdfPCell mergeColAndRow(String text, Font font, Boolean isHorizontal, Boolean isVertical, float height, int col, int row) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(height);
        if (isHorizontal) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
        if (isVertical) {
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        }
        //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格
        cell.setColspan(col);
        cell.setRowspan(row);
        return cell;
    }

    public static PdfPCell mergeColAndRow(Image image, int col, int row) {
        PdfPCell cell;
        if (image != null) {
            cell = new PdfPCell(image);
        } else {
            cell = new PdfPCell();
        }
        cell.setMinimumHeight(40);
        cell.setRowspan(row);
        cell.setColspan(col);
        return cell;
    }

    public static PdfPCell mergeColAndRow(String text, Font font, int col, int row) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, font));
        cell.setMinimumHeight(40);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格
        cell.setColspan(col);
        cell.setRowspan(row);
        return cell;
    }

    //页码事件
    public static class PdfPageEvent extends PdfPageEventHelper {
        private PdfTemplate template;
        private BaseFont bfChinese;

        @Override
        public void onOpenDocument(PdfWriter writer, Document document) {
            template = writer.getDirectContent().createTemplate(500, 500);
            try {
                bfChinese = BaseFont.createFont("/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            // 新建获得用户页面文本和图片内容位置的对象
            PdfContentByte pdfContentByte = writer.getDirectContent();
            // 保存图形状态
            pdfContentByte.saveState();
            String text = writer.getPageNumber() + "/";
            BaseFont baseFont = bfChinese;
            try {
                baseFont = BaseFont.createFont("/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
            // 获取点字符串的宽度
            float textSize = baseFont.getWidthPoint(text, 10);
            pdfContentByte.beginText();
            // 设置随后的文本内容写作的字体和字号
            pdfContentByte.setFontAndSize(baseFont, 10);
            // 定位'X/'
            float x = (document.right() + document.left()) / 2;
            float y = 10f;
            pdfContentByte.setTextMatrix(x, y);
            pdfContentByte.showText(text);
            pdfContentByte.endText();

            // 将模板加入到内容（content）中- // 定位'Y'
            pdfContentByte.addTemplate(template, x + textSize, y);

            pdfContentByte.restoreState();

            // 加入水印
            PdfContentByte waterMar = writer.getDirectContentUnder();
            // 开始设置水印
            waterMar.beginText();
            // 设置水印透明度
            PdfGState gs = new PdfGState();
            // 设置填充字体不透明度为0.3f
            gs.setFillOpacity(0.3f);
            BaseFont font = bfChinese;
            try {
                font = BaseFont.createFont("/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
            waterMar.setFontAndSize(font, 14);
            // 设置透明度
            waterMar.setGState(gs);
            // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
            waterMar.showTextAligned(Element.ALIGN_CENTER, "内蒙古自治区纪委监委驻内蒙古自治区农村信用社联合社纪检监察组",
                document.getPageSize().getWidth() / 2 + 50, document.getPageSize().getHeight() / 2 - 225, 45);
            waterMar.showTextAligned(Element.ALIGN_CENTER, "内蒙古自治区纪委监委驻内蒙古自治区农村信用社联合社纪检监察组",
                document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2 - 75, 45);
            waterMar.showTextAligned(Element.ALIGN_CENTER, "内蒙古自治区纪委监委驻内蒙古自治区农村信用社联合社纪检监察组",
                document.getPageSize().getWidth() / 2 - 50, document.getPageSize().getHeight() / 2 + 75, 45);
            waterMar.showTextAligned(Element.ALIGN_CENTER, "内蒙古自治区纪委监委驻内蒙古自治区农村信用社联合社纪检监察组",
                document.getPageSize().getWidth() / 2 - 100, document.getPageSize().getHeight() / 2 + 225, 45);

            // 设置水印颜色
            waterMar.setColorFill(BaseColor.GRAY);
            //结束设置
            waterMar.endText();
            waterMar.stroke();
        }

        /**
         * 重写PdfPageEventHelper中的onCloseDocument方法
         */
        @Override
        public void onCloseDocument(PdfWriter writer, Document document) {
            template.beginText();
            try {
                bfChinese = BaseFont.createFont("/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                template.setFontAndSize(bfChinese, 10);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
            template.setTextMatrix(0, 0);
            // 设置总页数的值到模板上，并应用到每个界面
            template.showText(String.valueOf(writer.getPageNumber()));
            template.endText();
        }
    }

}
